<?php

/**
 *      [CodeJm!] Author CodeJm[codejm@163.com].
 *
 *
 *      $Id: Order.php 2016-03-31 15:42:44 codejm $
 */
class OrderModel extends \Core_CMode
{

    public $_table = '`yaf_order`';

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => '序号',
            'order_number' => '订单号',
            'carry_type' => '搬家业务',
            'client_type' => '客服类型',
            'client_name' => '联系人',
            'client_phone' => '手机',
            'start_address' => '出发地址',
            'arrive_address' => '到达地址',
            'scg_id' => '附加费id',
            'item_id' => '大件物品id',
            'carrydate' => '搬运日期',
            'is_bill' => '发票',
            'total_price' => '总共价格',
            'already_price' => '已支付价格',
            'order_state' => '订单状态 0未确认，1已确认，2已派工，3完成',
            'remark' => '备注',
            'createtime' => '创建日期',
            'combo_id' => '套餐id',
            'deposit_rate' => '定金比例',
            'already_price' => '已支付',
            'final_payment' => '确认尾款',
            'must_price' => '已付金额',
            'driver_name' => '司机名称',
            'driver_phone' => '司机手机',
            'car_number' => '车牌号码',
            'foreman' => '领班人员',
            'openID' => '微信ID'
        );
    }

    /**
     * 验证
     *
     */
    public function rules()
    {
        return array(
            'default' => array(
                array('order_number', 'maxlength[30]'),
                array('carry_type', 'maxlength[1]|integer'),
                array('client_type', 'required|maxlength[1]'),
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('carrydate', 'required|maxlength[20]'),
                array('is_bill', 'maxlength[1]|integer'),
                array('createtime', 'required|maxlength[20]')
            ),

            'only_addback' => array(
                array('client_type', 'required|maxlength[1]'),
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('carrydate', 'required|maxlength[20]'),
            ),

            'only_addback2' => array(
                array('client_type', 'required|maxlength[1]'),
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('carrydate', 'required|maxlength[20]'),
                array('openID', 'required')
            ),

            'only_combo' => array(
                array('combo_id', 'required'),
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('carrydate', 'required|maxlength[20]')
            ),

            'only_edit' => array(
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('carrydate', 'required|maxlength[20]')
            ),
            'only_state0' => array(
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                //array('deposit_rate', 'integer|maxlength[2]'),
                array('carrydate', 'required|maxlength[20]'),
                array('must_price', 'required|float[20]|greaterthan[0]'),
                array('remark', 'maxlength[250]'),
                array('is_bill', 'required|integer')
            ),
            'only_state1' => array(
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('carrydate', 'required|maxlength[20]'),
                array('driver_name', 'required|maxlength[20]'),
                array('driver_phone', 'required|maxlength[11]|phone'),
                array('car_number', 'required|carnumber'),
                array('foreman', 'required|maxlength[30]'),
                array('remark', 'maxlength[250]')
            ),
            'only_state2' => array(
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('remark', 'maxlength[250]'),
                array('final_payment', 'float|required')
            ),
            'only_state3' => array(
                array('remark', 'maxlength[250]'),
            ),
            'only_reseting' => array(
                array('client_name', 'required|maxlength[30]'),
                array('client_phone', 'required|maxlength[11]|phone'),
                array('start_address', 'required|maxlength[100]'),
                array('arrive_address', 'required|maxlength[100]'),
                array('remark', 'maxlength[250]')
            )


        );
    }
    
    public function OrderCount() {
        $query = 'select order_state,order_place from '.$this->_table;
        $query .= 'where order_state in(0,1,2)';
        $dbconn = $this->db->query($query);
        $row=$dbconn->fetchAll(PDO::FETCH_ASSOC);
        $dbconn->closeCursor();
        return $row;
    }
    
    //7天内订单
    public function weekOrder() {
        $weektime =  strtotime('-7 days');
        $query = 'select must_price,already_price,final_payment,card_fee from '.$this->_table;
        $query .= 'where createtime > '.$weektime;
        $dbconn = $this->db->query($query);
        $row=$dbconn->fetchAll(PDO::FETCH_ASSOC);
        $dbconn->closeCursor();
        return $row;
    }
    
    //7天内订单
    public function weekfinshOrder() {
        $weektime =  strtotime('-7 days');
        $query = 'select final_payment_time,order_state from '.$this->_table;
        $query .= 'where final_payment_time > '.$weektime;
        $dbconn = $this->db->query($query);
        $row=$dbconn->fetchAll(PDO::FETCH_ASSOC);
        $dbconn->closeCursor();
        return $row;
    }

    /**
     * 鎵归噺鍒犻櫎
     *
     */
    public function delOrders($ids)
    {
        $ids_str = implode(',', array_fill(0, count($ids), '?'));
        $where = 'id IN (' . $ids_str . ')';
        return $this->update($where, array('status' => -1), $ids);
    }

    //搜索特定时间内的订单信息
    public function getOrderInTime($start, $end)
    {
        $start_time = strtotime($start);
        $end_time = strtotime($end);

        //SQL
        $sql = 'select * from ' . $this->_table . ' where createtime>=:start and createtime<=:end';
        $result = $this->db->prepare($sql);
        $result->execute(array('start' => $start_time, 'end' => $end_time));
        //$result->execute(array('start' => $start, 'end' => $end));
        $data = $result->fetchAll(PDO::FETCH_ASSOC);
        return $data;
    }
    
    //记录操作历史
    public function savelog($data)
    {
        $this->_table = 'yaf_action_log';
        $result =  $this->insert($data);
        $this->_table = '`yaf_order`';
        return $result;
        
    }
    
    //记录操作历史
    public function getlog($where)
    {
        $this->_table = 'yaf_action_log';
        $result =  $this->getLists($where);
        $this->_table = '`yaf_order`';
        return $result;
        
    }
}

?>
